package com.yueqian.controller;

import com.yueqian.domain.Product;
import com.yueqian.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("findAll.do")
    public ModelAndView findAll() throws Exception{
        System.out.println("ProductController.....findAll");
        List<Product> products = productService.findAll();
        System.out.println("ProductController: "+products);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("product-list"); //   /pages/product-list.jsp
        mav.addObject("productList",products);
        return mav;
    }

    @RequestMapping("main.do")
    public String mainJsp(){
        return "main";
    }

    @RequestMapping("save.do")
    public String save(Product product){
        System.out.println("ProductController"+product);
        //调用服务层保存数据
        productService.save(product);
        return "redirect:/product/findAll.do";
    }
}
